//
//  XYImageUtils.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/3/10.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYImageUtils.h"
#import "Color.h"
@implementation XYImageUtils
+ (UIImage *)image:(UIImage *)srcImage{
    NSUInteger width = CGImageGetWidth(srcImage.CGImage);
    NSUInteger height = CGImageGetHeight(srcImage.CGImage);
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    UInt32* imgaePixel = (UInt32*)calloc(width * height, sizeof(UInt32));
    //一个像素点 ARGB  = 4 * 8 = 32位 = 4字节
    CGContextRef contextRef = CGBitmapContextCreate(imgaePixel, width, height, 8, 4 * width, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    //根据图片上下文绘制图片
    CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), srcImage.CGImage);
    int ld = 50;
    for (int y = 0; y < height ; y++) {
        for (int x = 0; x < width; x++) {
            UInt32* currentPiexls = imgaePixel + (y *width) + x;
            UInt32 thisR, thisG, thisB, thisA;
            UInt32 color = *currentPiexls;
            thisR = Red(color);
            thisG = Green(color);
            thisB = Blue(color);
            thisA = Alpha(color);
            thisR += ld;
            thisG += ld;
            thisB += ld;
            
            *currentPiexls = RGBAMask(thisR, thisG, thisB, thisA);
        }
    }
    CGImageRef newImageRef = CGBitmapContextCreateImage(contextRef);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    CGColorSpaceRelease(colorSpaceRef);
    CGContextRelease(contextRef);
    
    return newImage;
}
@end
